package com.scott.classicthreadmodal.demo1.consumer;

import com.scott.classicthreadmodal.Product;
import com.scott.classicthreadmodal.demo1.storage.Storage;

public class Consumer implements Runnable {

    /**
     * 消费者名称
     */
    private String name;


    /**
     * 仓库
     */
    private Storage storage = null;

    public Consumer(String name, Storage storage) {
        this.name = name;
        this.storage = storage;
    }


    /**
     * 消费
     */
    @Override
    public void run() {
        try {
            while (true) {
                System.out.printf(Thread.currentThread().getName()+":[消费者](%s)准备消费产品.\n",name);
                Product product = storage.pop();
                System.out.printf(Thread.currentThread().getName()+":[消费者](%s)已消费(%d)\n",this.name,  product.getId());
                System.out.println(Thread.currentThread().getName()+":===============");
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}